﻿using Inovout.Component;
using Inovout.Data.Service;
using Inovout.Organization.Domain;
using Inovout.Web.Mvc.Security;
using Microsoft.Owin.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace KuaJing.LaiBa.JoinNow.WebPage.Controllers
{
    public class AccountController : OAuthAccountController
    {
        IRepositoryService<OrganizationUnit> organizationUnitService;

        public AccountController()
        {
            organizationUnitService = ComponentRegistry.Resolve<IRepositoryService<OrganizationUnit>>();
        }
       
        public ActionResult SignIn(string returnUrl)
        {
            if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                return Redirect(returnUrl);
            }
            else 
            {
                return base.External("Inovout","", returnUrl);
            }

        }

        [AllowAnonymous]
        public JsonResult GetOrgizaions()
        {
            var res = new JsonResult();
            List<object> listOrg = new List<object>();
            foreach (OrganizationUnit organizationUnit in organizationUnitService.Query().ToList())
            {
                listOrg.Add(new { Name = organizationUnit.Name, Id = organizationUnit.Id });
            }
            res.Data = listOrg;
            
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
          
            return res;
        }
    }
}